package com.cwt_taxi_xa.db.dal;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.cwt_taxi_xa.util.AppLog;
import com.cwt_taxi_xa.util.ExceptionUtil;
/**
 * 数据库工具类
 * @author wlh
 *
 */
public class DBOpenHelper extends SQLiteOpenHelper{

	private static String dataBaseName = "xaczc20180915.db";

	/**
	 * 修改数据库，必须升级数据库，递增版本号
	 */
	public static int DB_CURRENT_VERSION = 1;
	private final String TAG = "DBOpenHelper";
	private Context context;

	public DBOpenHelper(Context context){
		super(context, dataBaseName, null, DB_CURRENT_VERSION);
		this.context = context;
	}

	public boolean deleteDB(){
		boolean result = context.deleteDatabase(dataBaseName);
		return result;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		try {
			AppLog.i(TAG, "onCreate,versionCode:"+db.getVersion());
			onUpgrade(db, 0, DB_CURRENT_VERSION);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			AppLog.e(ExceptionUtil.getInfo(e), e);
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		for(int i = oldVersion;i<newVersion;i++){
			AppLog.i(TAG, "oldVersion:"+oldVersion+" ; newVersion:"+newVersion);
			DatabaseUpdater.update(db, i);
			AppLog.i(TAG, "onUpgrade,versionCode:"+db.getVersion());
		}
	}
}
